package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import org.springframework.util.StringUtils;

@Slf4j
@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Autowired
    AttrAttrgroupRelationService relationService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long categoryId) {
        //获取查询参数
        String key = (String) params.get("key");
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>();
        if(categoryId !=0){
            queryWrapper.eq("catelog_id", categoryId);
        }
        log.info("根据分类ID查询属性信息：{}", categoryId);
        //key搜索关键字如果为0查询所有
        if(!StringUtils.isEmpty(key) && !key.equals("0")){
            queryWrapper.and(wrapper -> {
               return wrapper.eq("attr_id", key).or().like("attr_name", key);
            });
        }
        log.info("key值{}", StringUtils.isEmpty("key"));//false
        log.info("key=0:{}", StringUtils.isEmpty("0"));//false
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
        PageUtils pageUtils = new PageUtils(page);
        //处理分组和分类信息
        List<AttrEntity> records = page.getRecords();
        List<AttrVo> list = records.stream().map(item -> {
            // AttrRespVo attrRespVo = new AttrRespVo();
            // //copy对应属性
            // BeanUtils.copyProperties(item, attrRespVo);
            // //处理分组信息
            //
            // //处理分类信息
            // Long catelogId = item.getCatelogId();
            // Long[] categoryIds = categoryService.findCategoryIds(categoryId);
            // attrRespVo.setCatelogPath(categoryIds);
            AttrVo attrVo = new AttrVo();
            BeanUtils.copyProperties(item, attrVo);
            //查询分组信息
            // relationService.getById(item.getAttrId())
            QueryWrapper<AttrAttrgroupRelationEntity> relationEntityQueryWrapper = new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", item.getAttrId());
            AttrAttrgroupRelationEntity relationEntity = relationService.getOne(relationEntityQueryWrapper);
            log.info("分组关联表：{}", relationEntity);
            if(relationEntity != null){
                AttrGroupEntity byId = attrGroupService.getById(relationEntity.getAttrGroupId());
                log.info("分组数据：{}", byId);
                if(byId != null){
                    attrVo.setAttrGroupName(byId.getAttrGroupName());
                }
            }
            //查询分类信息
            CategoryEntity categoryEntity = categoryService.getById(attrVo.getCatelogId());
            if(categoryEntity != null){
                attrVo.setCatelogName(categoryEntity.getName());
            }
            return attrVo;
        }).collect(Collectors.toList());
        pageUtils.setList(list);
        return pageUtils;
    }

    @Override
    public void saveDetail(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        //copy对应的属性
        BeanUtils.copyProperties(attr, attrEntity);
        //保存自己的数据
        this.save(attrEntity);
        //保存关联关系
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attr.getAttrGroupId());
        relationEntity.setAttrId(attrEntity.getAttrId());
        relationService.save(relationEntity);
    }

    @Override
    public AttrRespVo getDetail(Long attrId) {
        //查询规格参数
        AttrEntity attrEntity = baseMapper.selectById(attrId);

        AttrRespVo attrRespVo = new AttrRespVo();
        //copy对应属性
        BeanUtils.copyProperties(attrEntity, attrRespVo);

        //查询分组id
        QueryWrapper<AttrAttrgroupRelationEntity> relationEntityQueryWrapper = new QueryWrapper<>();
        relationEntityQueryWrapper.eq("attr_id", attrId);
        AttrAttrgroupRelationEntity relationEntity = relationService.getOne(relationEntityQueryWrapper);
        if(relationEntity != null){
            attrRespVo.setAttrGroupId(relationEntity.getAttrGroupId());
        }

        //查询分类ids
        Long[] categoryIds = categoryService.findCategoryIds(attrEntity.getCatelogId());
        if(categoryIds != null){
            attrRespVo.setCatelogPath(categoryIds);
        }
        return attrRespVo;
    }

}